package com.ruoyi.system.service.impl;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.entity.SysDictType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.mybatisPlus.BaseServiceImpl;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.mapper.SysNoticeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.SysOperLog;
import com.ruoyi.system.mapper.SysOperLogMapper;
import com.ruoyi.system.service.ISysOperLogService;

/**
 * 操作日志 服务层处理
 * 
 * @author ruoyi
 */
@Service
public class SysOperLogServiceImpl extends BaseServiceImpl<SysOperLogMapper, SysOperLog> implements ISysOperLogService
{
    @Autowired
    private SysOperLogMapper operLogMapper;

    /**
     * 新增操作日志
     * 
     * @param operLog 操作日志对象
     */
    @Override
    public void insertOperlog(SysOperLog operLog)
    {
        operLog.setOperTime(DateUtils.getNowDate());
        operLogMapper.insert(operLog);
    }

    /**
     * 查询系统操作日志集合
     * 
     * @param operLog 操作日志对象
     * @return 操作日志集合
     */
    @Override
    public List<SysOperLog> selectOperLogList(SysOperLog operLog) {
//        return operLogMapper.selectOperLogList(operLog);
        LambdaQueryWrapper<SysOperLog> wrapper = new LambdaQueryWrapper<>();

        // 构建查询条件（与上面相同）
        wrapper.like(ObjectUtil.isNotEmpty(operLog.getOperIp()), SysOperLog::getOperIp, operLog.getOperIp())
                .like(ObjectUtil.isNotEmpty(operLog.getTitle()), SysOperLog::getTitle, operLog.getTitle())
                .like(ObjectUtil.isNotEmpty(operLog.getOperName()), SysOperLog::getOperName, operLog.getOperName())
                .eq(ObjectUtil.isNotEmpty(operLog.getStatus()), SysOperLog::getStatus, operLog.getStatus())
                .eq(ObjectUtil.isNotEmpty(operLog.getBusinessType()), SysOperLog::getBusinessType, operLog.getBusinessType());

        if (operLog.getBusinessTypes() != null && operLog.getBusinessTypes().length>0) {
            wrapper.in(SysOperLog::getBusinessType, ListUtil.of(operLog.getBusinessTypes()));
        }

        // 处理时间范围查询
        if (ObjectUtil.isNotNull(operLog.getParams())) {
            String beginTimeString = (String) operLog.getParams().get("beginTime");
            String endTimeString = (String) operLog.getParams().get("endTime");

            if (ObjectUtil.isNotEmpty(beginTimeString)) {
                wrapper.ge(SysOperLog::getOperTime, DateUtils.parseDate(beginTimeString));
            }
            if (ObjectUtil.isNotEmpty(endTimeString)) {
                wrapper.le(SysOperLog::getOperTime, DateUtils.parseDate(endTimeString));
            }
        }

        wrapper.orderByDesc(SysOperLog::getOperId);
        return operLogMapper.selectList(wrapper);
    }

    /**
     * 批量删除系统操作日志
     * 
     * @param operIds 需要删除的操作日志ID
     * @return 结果
     */
    @Override
    public int deleteOperLogByIds(Long[] operIds)
    {
//        return operLogMapper.deleteOperLogByIds(operIds);
        for (Long operId : operIds) {
            operLogMapper.deleteById(operId);
        }
        return operIds.length;
    }

    /**
     * 查询操作日志详细
     * 
     * @param operId 操作ID
     * @return 操作日志对象
     */
    @Override
    public SysOperLog selectOperLogById(Long operId)
    {
//        return operLogMapper.selectOperLogById(operId);
        return operLogMapper.selectById(operId);
    }

    /**
     * 清空操作日志
     */
    @Override
    public void cleanOperLog()
    {
//        operLogMapper.cleanOperLog();
        LambdaQueryWrapper<SysOperLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.notIn(SysOperLog::getOperId, ListUtil.of(0L));
        operLogMapper.delete(wrapper);
    }
}
